package srm511;

public class GameOfLifeDivTwo {
    public String theSimulation(String init, int T) {
        int[] game = new int[init.length()];
        int[] past = new int[init.length()];

        for (int i = 0; i < game.length; i++) {
            game[i] = init.charAt(i) == '1' ? 1 : 0;
            past[i] = init.charAt(i) == '1' ? 1 : 0;
        }
        int len = past.length;
        int a, b, c;
        for (int i = 0; i < T; i++) {
            System.arraycopy(game, 0, past, 0, game.length);
            for (int j = 0; j < len; j++) {
                if (j - 1 < 0) a = past[len - 1];
                else a = past[j - 1];
                b = past[j];
                c = past[(j + 1) % len];
                if (a + b + c >= 2) {
                    game[j] = 1;
                } else {
                    game[j] = 0;
                }
            }
        }

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < len; i++) {
            sb.append(game[i]);
        }
        return sb.toString();
    }
}